require 'cgi'

class Manage::PagesController < ApplicationController
  include ActionView::Helpers::TextHelper
  include Security::Session::Access
  include Utils::Url
  include Utils::Text

  uses_tiny_mce(:options => {:theme => 'advanced',
                :browsers => %w{msie gecko opera},
                :flash_wmode => "transparent",
                :theme_advanced_toolbar_location => "top",
                :theme_advanced_toolbar_align => "left",
                :theme_advanced_resizing => true,
                :theme_advanced_resize_horizontal => false,
                :paste_auto_cleanup_on_paste => true,
                :theme_advanced_buttons1 => %w{formatselect fontselect fontsizeselect bold italic underline strikethrough separator justifyleft justifycenter justifyright indent outdent},
                :theme_advanced_buttons2 => [%w(spellchecker bullist numlist forecolor backcolor separator link unlink undo redo code flash)],
                :theme_advanced_buttons3 => [],
                :plugins => %w{spellchecker contextmenu paste advlink flash} })
                #:execcommand_callback => "myCustomExecCommandHandler"})

  def list
    @pages = Page.list_by_folder
  end

  def refine_list
    @pages = Page.search_for_values(params[:value])  
    render(:partial => "list_data", :layout => false)
  end

  def create
    case request.method
    when :post
      if params[:commit] == "Cancel"
        redirect_to(:action => "list")
        return
      end
      params[:page].delete(:id)
      @page = Page.new(params[:page])
      @page.updated_by = current_updater_id
      if @page.save
        gather_folder_info
        flash[:notice] = "Page successfully created"
        redirect_to(:action => "show", :id => @page.id)
      end
    else
      @page = Page.new
      if id_param
        p = Page.find(id_param) 
        @page.content = p.content
      end
      @page.enabled = true
    end
  end

  def show
    @page = Page.find(id_param)
  end

  def update
    case request.method
    when :post
      if params[:commit] == "Cancel"
        redirect_to(:action => "list")
        return
      end
      @page = Page.find(params[:page][:id])
      params[:page].delete(:id)
      @page.attributes = params[:page]
      @page.updated_by = current_updater_id
      if @page.save 
        gather_folder_info
        flash[:notice] = "Page successfully updated"
        redirect_to(:action => "show", :id => @page.id)
      end
    else
      @page = Page.find(id_param)
    end
  end

  def delete
    @page = Page.find(id_param)
    @page.destroy
    redirect_to(:action => "list")
  end

  private
    def gather_folder_info
      if empty_or_nil?(params[:page_folder_id])
        @page.errors.add_to_base("Folder is required")
        return
      end
      return if @page.folder.length > 0 && @page.folder[0].id == params[:page_folder_id].to_i
      FolderContent.delete_all("page_id = #{@page.id}")
      # << isn't supported
      #@page.folder << Folder.find(params[:page_folder_id])
      #TODO: update when new release prolly 1.1.7 supports this feature
      fldr = Folder.find(params[:page_folder_id])
      seq = FolderContent.get_next_seq(fldr.id)
      FolderContent.create(:folder_id => fldr.id, :page_id => @page.id, :seq => seq)
      fldr.assign_page_to_groups(@page)
    end
end
